<template>
  <div class="app-container">
    <div class="header">
      <el-form :inline="true" :model="queryParams" ref="ruleFormRef">
        <el-form-item label="分组名称" prop="groupName">
          <el-input v-model="queryParams.groupName" placeholder="请输入分组名称" />
        </el-form-item>
        <el-form-item label="分组编号" prop="groupOrder">
          <el-input v-model="queryParams.groupOrder" placeholder="请输入分组编号">
          </el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="分组状态"
            clearable
            style="width: 100px"
            @keyup.enter.native="getPageList"
          >
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getPageList" icon="Search">查询</el-button>
          <el-button icon="Refresh" @click="reset(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="footer">
      <div class="util">
        <el-button type="primary" icon="CirclePlus" @click="handleAdd" v-hasPermi="['gather:group:add']">新 增</el-button>
        <el-button type="info" icon="Delete" :disabled="multiple" @click="handleDelete(ids)" v-hasPermi="['gather:group:remove']">批量删除</el-button>
      </div>
      <div class="table-inner">
        <el-table v-loading="loading" :data="tableData" style="width: 100%" border  @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column label="ID" prop="groupId" width="100" align="center"></el-table-column>
          <el-table-column label="分组名称" prop="groupName" align="center"></el-table-column>
          <el-table-column label="分组编号" prop="groupOrder" align="center"></el-table-column>
          <el-table-column label="分组人数" prop="groupPeonum" width="100" align="center"></el-table-column>
          <el-table-column prop="state" label="状态" align="center" width="70px">
            <template #default="scope">
              <el-switch
                v-model="scope.row.status"
                @change="handleStatusChange(scope.row)"
                active-value="0"
                inactive-value="1"
                active-color="#599e5e"
                inactive-color="red"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="180px" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="更新时间" prop="updateTime" width="180px" align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="操作" width="270" align="center" fixed="right">
            <template #default="scope">
              <el-button type="primary" size="small" icon="Edit" @click="handlerEdit(scope.row)">
                编辑
              </el-button>
              <el-button type="danger" size="small" icon="Delete" @click="handleDelete(scope.row.groupId)">
                删除
              </el-button>
              <el-button type="success" size="small" icon="Plus"
                         @click="handleGroupNum(scope.row)">
                分配成员
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination">
        <!--分页器            -->
        <el-pagination
          v-show="total > 0"
          v-model:current-page="queryParams.pageNum"
          v-model:page-size="queryParams.pageSize"
          :page-sizes="[5, 10, 15, 20, 25, 30]"
          :background="true"
          layout="prev, pager, next, jumper, ->,  sizes, total"
          :total="total"
          @current-change="getList"
          @size-change="getList"
        />
      </div>
    </div>
  </div>
  <GroupDialog ref="groupDialogRef" @getList="getList"></GroupDialog>
  <UserGroupDialog ref="userGroupDialogRef" @getList="getList"></UserGroupDialog>
</template>
<script setup lang="ts">
import { getCurrentInstance, onMounted, reactive, ref } from "vue";
import { reqCrePeaPageUrl, reqCrePeaStartScoring } from "@/api/credit/peasants/pea-message";
import { ElMessageBox, FormInstance } from "element-plus";
import { reqAreaTreeSelect } from "@/api/system/user";
import { reqTableData } from "@/api/type";
import { useRouter } from "vue-router";
import { reqGroupChangeStatusUrl, reqGroupDeleteUrl, reqGroupPageListUrl } from "@/api/gather/group/index";
import GroupDialog from "@/views/system/group/components/GroupDialog.vue";
import { reqRoleChangeStatusUrl } from "@/api/system/role";
import UserGroupDialog from "@/views/system/group/components/UserGroupDialog.vue";

const { proxy }: any = getCurrentInstance();
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  groupName: undefined, // 姓名
  groupPeonum: undefined,
  groupOrder: undefined,
  status: undefined, /*状态*/
  beginTime: undefined, /*开始时间*/
  endTime: undefined, /*结束时间*/
});

const total = ref(0);
const loading = ref(true);
const tableData = ref();
const groupDialogRef = ref(null)
const userGroupDialogRef = ref(null)
// 信息收集对框框
const ruleFormRef = ref();
const $router = useRouter();


const ids = ref([]); // 选中数组
const single = ref(true); // 非单个禁用
const multiple = ref(true); //多个开启


/*多选框*/
const handleSelectionChange = (selection: any) => {
  ids.value = selection.map((item: any) => item.groupId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};


// 查询按钮
const getPageList = () => {
  queryParams.pageNum = 1;
  getList();
};

// 分页查询
const getList = async () => {
  loading.value = true;
  await reqGroupPageListUrl(queryParams).then((req: reqTableData) => {
    if (req.code == 200) {
      tableData.value = req.rows;
      total.value = req.total;
    } else {
      proxy.$modal.msgError(req.message);
    }
  });
  loading.value = false;
};

// 重置按钮
const reset = (formEl: FormInstance | undefined) => {
  // 将数据置空
  formEl.resetFields();
  Object.keys(queryParams).forEach((key) => {
    if(key === 'pageNum') queryParams[key] = 1
    else if(key === 'pageSize') queryParams[key] = 10
    else queryParams[key] = undefined;
  })
  // 再次进行查询
  getPageList();
};

// 新增
const handleAdd = () => {
  groupDialogRef.value.show(null);
};

// 修改
const handlerEdit = (item = {}) => {
  groupDialogRef.value.show(item);
};

/**
 * 状态修改
 * @param row
 */
const handleStatusChange = (row) => {
  let text = row.status === '0' ? '启用' : '停用'
  proxy.$modal
    .confirm("确认要\"" + text + "\"--\"" + row.groupName + "\"小组吗？")
    .then(async () => {
      /*发送异步请求*/
      await reqGroupChangeStatusUrl({groupId: row.groupId, status: row.status })
        .then((req: any) => {
          if (req.code === 200) {
            proxy.$modal.msgSuccess(text + '成功!')
          } else {
            proxy.$modal.msgError(text + '失败！' + req.message)
            row.status = row.status === '0' ? '1' : '0'
          }
        })
        .catch(() => {
          row.status = row.status === '0' ? '1' : '0'
        })
    })
    .catch(() => {
      row.status = row.status === '0' ? '1' : '0'
    })
};


// 删除用户
const handleDelete = (groupIds: any) => {
  proxy.$modal.confirm("你确定要删除" + groupIds + "项吗?")
    .then(async () => {
      // 进行删除操作
      await reqGroupDeleteUrl(groupIds).then((req) => {
        if (req.code == 200) {
          proxy.$modal.msgSuccess("删除成功");
          // 进行刷新
          getList();
        } else {
          proxy.$modal.msgSuccess("删除失败");
        }
      });
    })
    .catch((e) => {
      console.log("删除用户", e.message);
    });
};


// 分配小组成员
const handleGroupNum = (row: any) => {
  let groupId = row.groupId;
  $router.push('/gather/user-group/group/' + groupId)
}


onMounted(async () => {
  // 进行分页查询
  await getPageList();
});

</script>

<script lang="ts">
export default {
  dicts: ["sys_normal_disable", "pea_sex", "pea_finish_status", "pea_rating_status"]
};
</script>

<style scoped lang="scss">
@import './index';

.header {
  display: flex;
  justify-content: space-between;
  padding: 16px 16px 0px 16px;
  margin-bottom: 16px;
  border-radius: 4px;
  background: white;
  box-shadow: 0 0 12px rgb(0 0 0 / 5%);

  .header-span {
    margin-left: 10px;
    color: red;
  }

  .header-button {
    font-weight: bold;
  }
}

.footer {
  flex: 1;
  display: flex;
  padding: 16px;
  flex-direction: column;
  border-radius: 4px;
  overflow: hidden;
  background: white;
  box-shadow: 0 0 12px rgb(0 0 0 / 5%);
  position: relative;
  box-sizing: border-box;

  .util {
    margin-bottom: 15px;
    display: flex;
    justify-content: flex-end;
    flex-shrink: 0;
  }

  .table-inner {
    flex: 1;
    position: relative;
  }

  .table {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
  }
}
</style>
